package com.interfaces;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.web.Json;
import com.common.web.ReturnCodes;
import com.core.request.bo.VisaRequestBo;
import com.core.request.pojo.VisaCancelDto;
import com.core.request.pojo.VisaForDownloadReturnReportDto;
import com.core.request.pojo.VisaForImportReturnReportDto;
import com.core.request.pojo.VisaResponse;
import com.core.request.pojo.VisaAddUpdateDto;
import com.core.request.pojo.VisaAddUpdateDtoSql;
import com.core.sysConfig.bo.SysConfigBo;
import com.core.sysConfig.pojo.SysConfig;
import com.util.exception.ComRuntimeException;
import com.util.exception.ComSystemException;
import com.util.framework.BaseController;

@Controller
public class VisaRequestController extends BaseController {
	protected Logger log = Logger.getLogger(this.getClass());
	@Resource
	private VisaRequestBo visaRequestBo;

	@Autowired
	private SysConfigBo sysConfigBo;

	public static final String URL_ENTITY = "/visa_request";

	public static final String URL_GET_COOKIE = URL_ENTITY + "/get_cookie";

	// ------ visa
	public static final String URL_GET_VISA_FOR_ADD_UPDATE = URL_ENTITY + "/get_visa_for_add_update";
	public static final String URL_GET_VISA_FOR_CANCEL = URL_ENTITY + "/get_visa_for_cancel";
	public static final String URL_UPDATE_VISA_STATE = URL_ENTITY + "/update_visa_state";

	// ------ return_report
	public static final String URL_GET_VISA_FOR_IMPORT_RETURN_REPORT = URL_ENTITY + "/get_visa_for_import_return_report";
	public static final String URL_UPDATE_RETURN_REPORT_STATE = URL_ENTITY + "/update_return_report_state";

	public static final String URL_GET_VISA_FOR_DOWNLOAD_RETURN_REPORT = URL_ENTITY + "/get_visa_for_download_return_report";
	public static final String URL_UPDATE_RETURN_REPORT_DOWNLOAD_STATE = URL_ENTITY + "/update_return_report_download_state";

	// ========================= visa ==============================//
	@ResponseBody
	@RequestMapping(value = URL_GET_COOKIE, produces = { "application/json;charset=utf-8" })
	public Json<String> getCookie(HttpSession session) {
		Json<String> json = new Json<String>();
		json.setSuccess(true);
		try {
			SysConfig sysConfig = sysConfigBo.getByKeyNo("cookie");
			if (sysConfig != null) {
				json.setObj(sysConfig.getKey_value());
				json.setReturnCode(0);
			}
		} catch (ComRuntimeException e) {
			json = new Json<String>(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json<String>(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json<String>(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... " + e.getMessage());
		}

		return json;
	}

	@ResponseBody
	@RequestMapping(value = URL_GET_VISA_FOR_ADD_UPDATE, produces = { "application/json;charset=utf-8" })
	public Json<VisaAddUpdateDto> get_visa_for_add_update(HttpSession session) {
		Json<VisaAddUpdateDto> json = new Json<VisaAddUpdateDto>();
		json.setSuccess(true);
		try {
			VisaAddUpdateDtoSql visaDto = visaRequestBo.getVisaForAddUpdate();
			if (visaDto != null) {
				VisaAddUpdateDto visaSubmitDto = new VisaAddUpdateDto(visaDto);
				json.setObj(visaSubmitDto);
			}
		} catch (ComRuntimeException e) {
			json = new Json<VisaAddUpdateDto>(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json<VisaAddUpdateDto>(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json<VisaAddUpdateDto>(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... " + e.getMessage());
		}

		return json;
	}

	@ResponseBody
	@RequestMapping(value = URL_GET_VISA_FOR_CANCEL, produces = { "application/json;charset=utf-8" })
	public Json<VisaCancelDto> get_visa_for_cancel(HttpSession session) {
		Json<VisaCancelDto> json = new Json<VisaCancelDto>();
		json.setSuccess(true);
		try {
			VisaCancelDto visaCancelDto = visaRequestBo.getVisaForCancel();
			if (visaCancelDto != null) {
				json.setObj(visaCancelDto);
			}
		} catch (ComRuntimeException e) {
			json = new Json<VisaCancelDto>(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json<VisaCancelDto>(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json<VisaCancelDto>(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... " + e.getMessage());
		}

		return json;
	}

	@ResponseBody
	@RequestMapping(value = URL_UPDATE_VISA_STATE, produces = { "application/json;charset=utf-8" })
	public Json<Object> update_visa_state(VisaResponse visaResponse, HttpSession session) {
		Json<Object> json = new Json<Object>();
		json.setSuccess(true);
		try {
			visaRequestBo.updateVisaState(visaResponse);
		} catch (ComRuntimeException e) {
			json = new Json<Object>(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json<Object>(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json<Object>(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... " + e.getMessage());
		}
		return json;
	}

	// =========================return_report==============================//
	@ResponseBody
	@RequestMapping(value = URL_GET_VISA_FOR_IMPORT_RETURN_REPORT, produces = { "application/json;charset=utf-8" })
	public Json<VisaForImportReturnReportDto> getVisaForImportReturnReport() {
		Json<VisaForImportReturnReportDto> json = new Json<VisaForImportReturnReportDto>();
		json.setSuccess(true);
		try {
			VisaForImportReturnReportDto dto = visaRequestBo.getVisaForImportReturnReport();
			json.setObj(dto);
		} catch (ComRuntimeException e) {
			json = new Json<VisaForImportReturnReportDto>(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json<VisaForImportReturnReportDto>(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json<VisaForImportReturnReportDto>(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... " + e.getMessage());
		}

		return json;
	}

	@ResponseBody
	@RequestMapping(value = URL_UPDATE_RETURN_REPORT_STATE, produces = { "application/json;charset=utf-8" })
	public Json<Object> update_return_report_state(VisaResponse visaResponse, HttpSession session) {
		Json<Object> json = new Json<Object>();
		json.setSuccess(true);
		try {
			visaRequestBo.updateReturnReportState(visaResponse);
		} catch (ComRuntimeException e) {
			json = new Json<Object>(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json<Object>(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json<Object>(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... " + e.getMessage());
		}
		return json;
	}

	@ResponseBody
	@RequestMapping(value = URL_GET_VISA_FOR_DOWNLOAD_RETURN_REPORT, produces = { "application/json;charset=utf-8" })
	public Json<VisaForDownloadReturnReportDto> URL_GET_VISA_FOR_DOWNLOAD_RETURN_REPORT() {
		Json<VisaForDownloadReturnReportDto> json = new Json<VisaForDownloadReturnReportDto>();
		json.setSuccess(true);
		try {
			VisaForDownloadReturnReportDto dto = visaRequestBo.getVisaForDownloadReturnReport();
			json.setObj(dto);
		} catch (ComRuntimeException e) {
			json = new Json<VisaForDownloadReturnReportDto>(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json<VisaForDownloadReturnReportDto>(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json<VisaForDownloadReturnReportDto>(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... " + e.getMessage());
		}

		return json;
	}

	@ResponseBody
	@RequestMapping(value = URL_UPDATE_RETURN_REPORT_DOWNLOAD_STATE, produces = { "application/json;charset=utf-8" })
	public Json<Object> URL_UPDATE_RETURN_REPORT_DOWNLOAD_STATE(VisaResponse visaResponse, HttpSession session) {
		Json<Object> json = new Json<Object>();
		json.setSuccess(true);
		try {
			visaRequestBo.updateReturnReportDownloadState(visaResponse);
		} catch (ComRuntimeException e) {
			json = new Json<Object>(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json<Object>(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json<Object>(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... " + e.getMessage());
		}
		return json;
	}

}
